package com.xiaowei.gym.gymjsp.ware.filter;


import com.xiaowei.gym.gymjsp.ware.entity.AdminEntity;
import com.xiaowei.gym.gymjsp.ware.util.Constants;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/gymjsp/*")
public class SysFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest rq = (HttpServletRequest) servletRequest;
        HttpServletResponse rp = (HttpServletResponse) servletResponse;
        //过滤器，从session中获取用户
        AdminEntity userSession = (AdminEntity) rq.getSession().getAttribute(Constants.USER_SESSION);
        System.out.println();
        if (rq.getRequestURI().endsWith("/contact.jsp") ||
                rq.getRequestURI().endsWith("/members.jsp") ||
            rq.getRequestURI().endsWith("schedule.jsp")){
            filterChain.doFilter(servletRequest,servletResponse);
        } else if (null == userSession && (!rq.getRequestURI().endsWith("/error.jsp"))) {
            rp.sendRedirect(rq.getContextPath()+"/gymjsp/error.jsp");
        } else {
            filterChain.doFilter(servletRequest,servletResponse);
        }
    }
}
